﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace VisionApp.Startup.Views;

public class BaseWindow : Window
{
    public BaseWindow()
    {
        // 在窗口初始化时，自动应用我们定义的全局样式
        this.Style = (Style)Application.Current.FindResource("CustomWindowStyle");
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        // 在模板应用后，查找按钮并为它们附加点击事件
        if (GetTemplateChild("CloseButton") is Button closeButton)
        {
            closeButton.Click += (s, e) => this.Close();
        }

        if (GetTemplateChild("MinimizeButton") is Button minimizeButton)
        {
            minimizeButton.Click += (s, e) => this.WindowState = WindowState.Minimized;
        }

        if (GetTemplateChild("MaximizeButton") is Button maximizeButton)
        {
            maximizeButton.Click += (s, e) => this.WindowState =
                this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }
    }

    // 重写鼠标左键按下事件，以实现窗口拖动
    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        // 检查鼠标点击位置是否在标题栏区域 (Y <= 32)
        if (e.ButtonState == MouseButtonState.Pressed && e.GetPosition(this).Y <= 32)
        {
            this.DragMove();
        }
    }

    protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
    {
        base.OnMouseDoubleClick(e);

        // 检查鼠标点击位置是否在标题栏区域 (Y <= 32)
        if (e.ButtonState == MouseButtonState.Pressed && e.GetPosition(this).Y <= 32)
        {
            this.WindowState = this.WindowState==WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }
    }
}
